<?php
/**
 * Administra los Banners del sitio
  *@author Luis Gabriel Rodriguez <luisgabriel84@gmail.com>
 */

/**
 * Administra los banners del sitio
 */

class BannersController extends AppController {

	var $name = 'Banners';
        /**
        * @var array
        * @access public
        */
	var $helpers = array('Html', 'Form', 'Flash', 'Javascript', 'Time','Filetype');

        /**
        * @var array
        * @access public
        */
        var $components = array('Filehandler');
        
        function beforeFilter()
        {
            $this->Auth->allow('banners','slider','bannertop');
        }
	
	 /**
         *
         * Listado de banners para el administrador
         */
	function admin_index() 
	{
		$this->layout ='admin';
		$this->Banner->recursive = 1;	
		$this->set('banners', $this->paginate());
	}
	
        /**
         * Muestra el banner al administrador
         * @param integer $id Identificador del banner
         */
	function admin_view($id = null) {
		$this->layout ='admin';
		if (!$id) {
			$this->Session->setFlash(__('Invalid Banner.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('banner', $this->Banner->read(null, $id));
	}

        /**
         * Retorna los banners de la seccion superior
         * @return array banners
         */
        function bannertop()
	{
		if(isset($this->params['requested']))
		{
                        $departamento =  $this->Session->read('Config');
		        if(!array_key_exists('dep', $departamento))
	                {
	                    $banners = $this->Banner->find('all', array('conditions'=>array('superior'=>1,'slider'=>0),'order'=>'orden ASC'));

	                }
	                else
	                {

	                    $banners= $this->Banner->find('all', array('conditions'=>array('state_id'=>$departamento,'superior'=>1,'slider'=>0),'order'=>'orden ASC'));
	                }

			return $banners;

		}

	}



        /**
         * Muestra todos los banners por departamento
         * @return array banners
         */
	function banners()
	{
		if(isset($this->params['requested']))
		{
                        $departamento =  $this->Session->read('Config');
		        if(!array_key_exists('dep', $departamento))
	                {
	                    $banners = $this->Banner->find('all', array('conditions'=>array('en_portada'=>1,'slider'=>0),'order'=>'orden ASC'));
	
	                }
	                else
	                {
	                   
	                    $banners= $this->Banner->find('all', array('conditions'=>array('state_id'=>$departamento,'slider'=>0),'order'=>'orden ASC'));
	                }
                        
			return $banners;
		
		}
		
	}
	
	 /**
         * Muestra todos los banners del slider
         * @return array banners
         */
	function slider()
	{
		if(isset($this->params['requested']))
		{
                        $departamento =  $this->Session->read('Config');
                        
		        if(!array_key_exists('dep', $departamento))
	                {
	                    $banners = $this->Banner->find('all', array('conditions'=>array('en_portada'=>1,'slider'=>1),'order'=>'orden ASC'));
	                    
	                }
	                else
	                {
	                    
	                    $banners= $this->Banner->find('all', array('conditions'=>array('state_id'=>$departamento['dep'],'slider'=>1),'order'=>'orden ASC'));
	                }
                        
			return $banners;
		
		}
		
	}
	
	 /**
         * Agrega un nuevo banner
         *
         */
	function admin_add() {
		$this->layout ='admin';
		if (!empty($this->data)) {
			$this->Banner->create();
			
			if(!empty( $this->data ['Banner']['filename']['name'] ))
			{
                                $this->data['Banner']['filename'] = $this->Filehandler->upload_single_file('/files/banners/',$this->data['Banner']['filename']);		
			}
		
			
			
			if ($this->Banner->save($this->data)) {
				$this->Session->setFlash(__('The Banner has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Banner could not be saved. Please, try again.', true));
			}
		}
		$this->set('states',$this->Banner->State->find('list'));
	}
	
	 /**
         * Modificar un banner
         * @param intenger id identificador del banner
         */
	function admin_edit($id = null) {
		$this->layout ='admin';
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Banner', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
		    
            //Inicia actualizacicion de archivo           
            $this->data_update = $this->Banner->read(null, $id);
            if( !empty($this->data['Banner']['filename']['name']))
            {

                $this->Filehandler->deleteFile( '/files/banners/', $this->data_update['Banner']['filename']);
                $this->data['Banner']['filename'] = $this->Filehandler->upload_single_file('/files/banners/',$this->data['Banner']['filename']);
            
            }
            else
            {
                $this->data['Banner']['filename'] = $this->data_update['Banner']['filename']; 
            }
            //FIN actualizacicion de archivo
            
			if ($this->Banner->save($this->data)) {
				$this->Session->setFlash(__('El Banner ha sido guardado', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Banner could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Banner->read(null, $id);
		}
		$this->set('states',$this->Banner->State->find('list'));
	}
	
        /**
         * Elimina un banner
         * @param intenger id identificador del banner
         */
	function admin_delete($id = null) {
		$this->layout ='admin';
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Banner', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->data_update = $this->Banner->read(null, $id);    
		$file = $this->data_update['Banner']['filename'];
		
                $this->Filehandler->deleteFile( '/files/banners/', $this->data_update['Banner']['filename']);
		                
		if ($this->Banner->del($id)) {
			$this->Session->setFlash(__('Banner Eliminado', true));
			$this->redirect(array('action'=>'index'));
		}
	}
}
?>